package com.woke.method;

import android.databinding.BindingAdapter;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.nim.uikit.GlideApp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者 ： 暗网
 * 创建时间：2017/2/17
 * 备注：
 */

public class DataBindingUtils {
    @BindingAdapter("android:src")
    public static void setImageResource(ImageView imageView, int srcId) {
        if (srcId == 0 || srcId == -1) return;
        imageView.setImageResource(srcId);
    }

    @BindingAdapter("imageUrl")
    public static void loadImage(ImageView imageView, String url) {
        String imgUrl;
        if (TextUtils.isEmpty(url)) return;
        if (url.startsWith("http")) {
            imgUrl = url;
        } else {
            imgUrl = "http://" + url;
        }
        GlideApp.with(imageView.getContext())
                .load(imgUrl)
                .into(imageView);
    }

    //头像
    @BindingAdapter("headPic")
    public static void loadUserHead(final ImageView imageView, String url) {
        String imgUrl;
        if (TextUtils.isEmpty(url)) return;
        if (url.startsWith("http")) {
            imgUrl = url;
        } else {
            imgUrl = "http://" + url;
        }
        GlideApp.with(imageView.getContext())
                .load(imgUrl)
                .into(imageView);
    }


    @BindingAdapter("date")
    public static void loadImage(TextView textView, long create_time) {
        if (textView != null) {
            if (create_time > 0) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                textView.setText(sdf.format(new Date(create_time * 1000L)));
            }
        }
    }

}
